package com.wolfalpha.utron.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.base.BaseNormal4Activity;
import com.wolfalpha.utron.adapter.ItemProjectCommentAdapter;
import com.wolfalpha.utron.app.AppConfig;
import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.bean.ItemProjectComment;
import com.wolfalpha.utron.service.ProjectService;
import com.wolfalpha.utron.service.impl.ProjectServiceImpl;
import com.wolfalpha.utron.ui.UIHelper;
import com.wolfalpha.utron.utils.ThreadPoolUtils;
import com.wolfalpha.utron.view.XListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目评论界面
 * <p/>
 * Created by luodon on 15/9/1.
 */

public class ProjectCommentActivity extends BaseNormal4Activity implements XListView.IXListViewListener {

    private XListView lvComments;        //评论列表控件

    private int pId = 0;

    private ProjectService projectService;

    private List<ItemProjectComment> comments = new ArrayList<ItemProjectComment>();

    private ItemProjectCommentAdapter commentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        if (bundle == null || !bundle.containsKey("pId")) {
            finish();
            return;
        }
        pId = bundle.getInt("pId");

        setContentView(R.layout.activity_project_comment);
        projectService = new ProjectServiceImpl();

        intiView();
    }

    private int page = 1;

    /**
     * 初始化界面
     */
    private void intiView() {
        setTvTitle("评论详情");
        setRightText("发评论");
        lvComments = (XListView) findViewById(R.id.xl_project_comment);
        commentAdapter = new ItemProjectCommentAdapter(ProjectCommentActivity.this, comments, pId);
        lvComments.setAdapter(commentAdapter);
        lvComments.setPullLoadEnable(false);
        lvComments.setXListViewListener(this);
        lvComments.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("reply", comments.get(position - 1));
                bundle.putInt("pId", pId);
                UIHelper.showActivity(ProjectCommentActivity.this, ProjectReplyDetailActivity.class, bundle);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        onRefresh();
    }

    @Override
    public void rightOperation() {
        //项目评论
        if (!AppContext.getInstance().getIsLogined()) {
            UIHelper.showNotLogined(ProjectCommentActivity.this);
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putInt("pId", pId);
        UIHelper.showActivity(ProjectCommentActivity.this, ProjectReplyActivity.class, bundle);
    }

    @Override
    public void onRefresh() {
        page = 1;

        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                comments = projectService.getComments(pId, page, 0);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (comments != null) {
                            commentAdapter.setData(comments);
                            if (comments.size() < AppConfig.COMMENT_PAGE_SIZE) {
                                lvComments.setPullLoadEnable(false);
                            } else {
                                lvComments.setPullLoadEnable(true);
                            }
                        } else {
                            lvComments.setPullLoadEnable(false);
                        }
                        lvComments.stopRefresh();
                    }
                });
            }
        });
    }

    @Override
    public void onLoadMore() {
        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                final List<ItemProjectComment> comments1 = projectService.getComments(pId, ++page, 0);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (comments1 != null) {
                            comments.addAll(comments1);
                            commentAdapter.setData(comments);
                            if (comments1.size() < AppConfig.COMMENT_PAGE_SIZE) {
                                lvComments.setPullLoadEnable(false);
                            } else {
                                lvComments.setPullLoadEnable(true);
                            }
                        } else {
                            page--;
                            lvComments.setPullLoadEnable(false);
                        }
                        lvComments.stopLoadMore();
                    }
                });

            }
        });
    }
}
